/*
 * Copyright (C),2015,北京新诺创科软件技术有限公司
 * author zhangmengliang
 */
package com.xnck.example.jertest.util;


import com.xiaoleilu.hutool.DateTime;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

public class DateUtil extends com.xiaoleilu.hutool.DateUtil {

    /**
     * 获取某天的开始时间
     * @param date 日期
     * @return 某天的开始时间
     */
    public static DateTime getBeginTimeOfDay(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new DateTime(calendar.getTime());
    }

    /**
     * 获取某天的结束时间
     * @param date 日期
     * @return 某天的结束时间
     */
    public static DateTime getEndTimeOfDay(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 0);
        return new DateTime(calendar.getTime());
    }

    /**
     * 获取某月的第一天
     * @param date 日期
     * @return 第一天的开始时间
     */
    public static DateTime getFirstDay(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new DateTime(calendar.getTime());
    }

    /**
     * 获取某月的最后一天
     * @param date 日期
     * @return 最后一天的结束时间
     */
    public static DateTime getLastDay(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //获取某月最大天数
        int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        calendar.set(Calendar.DAY_OF_MONTH, lastDay);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 0);
        return new DateTime(calendar.getTime());
    }

    /**
     * 获取某年第一天日期
     * @param year 年份
     * @return Date
     */
    public static DateTime getYearFirst(int year){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        return new DateTime(calendar.getTime());
    }

    /**
     * 获取某年最后一天日期
     * @param year 年份
     * @return Date
     */
    public static DateTime getYearLast(int year){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 0);
        return new DateTime(calendar.getTime());
    }

    /**
     * 间隔多少年
     * @param startDate
     * @param endDate
     * @return
     */
    public static double getRangeYears(Date startDate, Date endDate){
        long days = DateUtil.diff(startDate, endDate, 1000 * 60 * 60 * 24);
        double months = ((double) days) / 365;
        months = getRoundHalfUp(months);
        return months;
    }

    /**
     * 间隔多少个月
     * @param startDate
     * @param endDate
     * @return
     */
    public static double getRangeMonths(Date startDate, Date endDate){
        long days = DateUtil.diff(startDate, endDate, 1000 * 60 * 60 * 24);
        double months = ((double) days) / 365 * 12;
        months = getRoundHalfUp(months);
        return months;
    }

    /**
     * 间隔多少天
     * @param startDate
     * @param endDate
     * @return
     */
    public static long getRangeDays(Date startDate, Date endDate){
        long days = DateUtil.diff(startDate, endDate, 1000 * 60 * 60 * 24);
        return days;
    }

    /**
     * 最大的时间
     * @return 2999-12-31 23:59:59
     */
    public static DateTime getMaxTime(){
        return parse("2999-12-31 23:59:59");
    }

    private static double getRoundHalfUp(double value){
        BigDecimal b = new BigDecimal(value);
        double f1 = getRoundHalfUp(b);
        return f1;
    }

    private static double getRoundHalfUp(BigDecimal value){
        double f1 = value.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return f1;
    }
}
